package com.xcf.controller.business.common;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.xcf.controller.base.BaseController;
import com.xcf.entity.system.Role;
import com.xcf.service.business.common.internalfee.InternalFeeManageService;
import com.xcf.util.Const;
import com.xcf.util.FileDownload;
import com.xcf.util.FileUpload;
import com.xcf.util.GetPinyin;
import com.xcf.util.Jurisdiction;
import com.xcf.util.ObjectExcelRead;
import com.xcf.util.PageData;
import com.xcf.util.PathUtil;
import com.xcf.util.Tools;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/** 
 * 港口费用管理
 * 类名称：InternalFeeManageController
 * 创建人：tanwei
 * 更新时间：2017年11月23日
 * @version
 */
@Controller
@RequestMapping("/internalFeeManage")
public class InternalFeeManageController extends BaseController{
	@Resource
	private InternalFeeManageService internalFeeManageService;
	String menuUrl = "internalFeeManage/list";
	/**
	 * 内贸费用管理列表查询
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public ModelAndView internalFeeManageList()throws Exception{
		ModelAndView view = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		List<PageData> internalFeeManage = internalFeeManageService.findInternalFeeManageInfoList(pd);
		view.addObject("internalFeeManage", internalFeeManage);
		view.setViewName("/business/common/internalfee/internal_fee_manage_list");
		return view;
		
	}
	/**
	 * 保存或者更新内贸管理数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("saveOrUpdateInternalFeeManage")
	@ResponseBody
	public String saveOrUpdateInternalFeeManage()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		if(pd.getString("ID")==null || pd.getString("ID")==""){
			internalFeeManageService.saveInternalFeeManageInfo(pd);
			}else{
				internalFeeManageService.updateInternalFeeManageInfo(pd);
			}
		
		return null;
		
	}
	/**
	 * 删除内贸费用管理数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteInternalFeeManage")
	@ResponseBody
	public String deleteInternalFeeManage()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		internalFeeManageService.deleteInternalFeeManageInfo(pd);
		return null;
		
	}
	/**
	 * 批量删除内贸费用管理数据
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteInternalFeeManageAll")
	@ResponseBody
	public String deleteInternalFeeManageAll() throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		String data = pd.getString("dataJsonStr");

		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			String id = item.getString("ID").trim();
			PageData pdq = new PageData();
			pdq.put("ID", id);
			internalFeeManageService.deleteInternalFeeManageInfo(pdq);
		}
		return null;
		
	}
	
	/**
	 * @author zhouheyou
	 * 打开上传EXCEL页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/goUploadExcel")
	public ModelAndView goUploadExcel()throws Exception{
		ModelAndView mv = this.getModelAndView();
		mv.setViewName("/business/common/internalfee/uploadexcel");
		return mv;
	}
	
	/**
	 * 
	 * @author zhouheyou
	 * 下载模版
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/downExcel")
	public void downExcel(HttpServletResponse response)throws Exception{
		FileDownload.fileDownload(response, PathUtil.getClasspath() + Const.FILEPATHFILE + "港口费用管理.xls", "港口费用管理.xls");
	}
	/**
	 * @author zhouheyou
	 * @param file
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/readExcel")
	public ModelAndView readExcel(
			@RequestParam(value="excel",required=false) MultipartFile file
			) throws Exception{
		ModelAndView mv = this.getModelAndView();
		String filePath = PathUtil.getClasspath() + Const.FILEPATHFILE;
		String fileName =  FileUpload.fileUp(file, filePath, "test");	
		List<PageData> listPd = (List)ObjectExcelRead.readExcel(filePath, fileName, 1, 0, 0);
		PageData pd = new PageData();
		for (PageData data : listPd) {
			pd.put("GOODS_TYPE", data.getString("var0"));//货物种类
			pd.put("BOX_TYPE", data.getString("var1"));//箱型
			pd.put("PROT_NAME", data.getString("var2"));//港口名称
			pd.put("FEE_NAME", data.getString("var3"));//费用名称
			pd.put("SETTLE_UNIT", data.getString("var4"));//结算单位
			pd.put("TRADE_TYPE", data.getString("var5"));//贸易类别
			pd.put("UNIT_PRICE", data.getString("var6"));//单价
			pd.put("TRANSPORT_CLAUSE", data.getString("var7"));//运输条款
			pd.put("EMPTY_SIGN", data.getString("var8"));//空重箱标志
			pd.put("LOAD_AND_UNLOAN_SIGN", data.getString("var9"));//装卸标志
			int enable=0;
			if(data.getString("var10").equals("是")){
				enable=1;
			}else{
				enable=0;
			}
			pd.put("ENABLE", enable);//是否可用
			pd.put("RIVER_BOAT_COMPANY", data.getString("var11"));//江船公司
			pd.put("START_PRICE_TIME", data.getString("var12"));//开始时间
			pd.put("END_PRICE_TIME", data.getString("var13"));//结束时间
		     PageData single = internalFeeManageService.finByone(pd);
		     if(single==null){
		    	 internalFeeManageService.saveInternalFeeManageInfo(pd);
		     }else{
		    	 internalFeeManageService.update(pd); 
		     }
			
		}
	
		mv.addObject("msg","success");
		mv.setViewName("/business/common/internalfee/save_result");
		return mv;
	}
}
